home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / EDUCAMES / NEWCLK10.LZH / NEWCLOCK.C next >
Text File  |  1991-03-01  |  11KB  |  423 lines

  1. /*
  2.    newclock.c by bill buckels
  3.    a clock program template
  4.    March 1991
  5.  
  6. */
  7.  
  8. /*
  9.  
  10. a radian is the measure of an angle with its with vertex at the
  11. center of a circle whose intercepted arc on the circle is equal
  12. in length to the radius of the circle...
  13.  
  14.  - allyn j. washington
  15.  
  16. */
  17.  
  18. #include <graphics.h>
  19. #include <bios.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22.  
  23. #define ESCKEY     27  /* character generated by the Esc key            */
  24. #define FUNCKEY    0   /* first character generated by function keys    */
  25. #define UPARROW    'H' /* second character generated by up-arrow key    */
  26. #define DOWNARROW  'P' /* second character generated by down-arrow key  */
  27. #define LEFTARROW  'K' /* second character generated by left-arrow key  */
  28. #define RIGHTARROW 'M' /* second character generated by right-arrow key */
  29.  
  30. struct vconfig _videostuff;
  31. float aspect_rat;
  32. char timestuff[2][40];
  33. char stuffcords[2][2]={0,182,
  34.                        0,24};
  35.  
  36. void circlepoints(x,y,baselength ,fdegrees)
  37. int *x,*y;
  38. int baselength;
  39. float fdegrees;
  40. {
  41.      /* convert from degrees to radians */
  42.      /* in order to use trig functions  */
  43.      float pi=3.1415926535;
  44.      float radians;
  45.      float tempx= (float)baselength;
  46.      float tempy= (float)baselength;
  47.      int degrees= (int)fdegrees;
  48.  
  49.      switch(degrees)
  50.      {
  51.        case 360 :
  52.        case 0   : *y-=(int)(aspect_rat*baselength);break;
  53.        case 180 : *y+=(int)(aspect_rat*baselength);break;
  54.  
  55.        case 90  : *x+=baselength;break;
  56.        case 270 : *x-=baselength;break;
  57.  
  58.      default    :
  59.  
  60.      /* positive angles - all quadrants */
  61.      if(degrees > 0  && degrees <=45  || degrees >= 315 && degrees < 360||
  62.         degrees > 90 && degrees <=135 || degrees >= 225 && degrees < 270)
  63.      {
  64.        radians = (pi*degrees)/180;
  65.        tempx*= sin(radians);
  66.        tempy*= cos(radians);
  67.        *x+=(int)tempx;
  68.        *y-=(int)(tempy*aspect_rat);
  69.       }
  70.  
  71.     /*  negative angles - all quadrants */
  72.      if(degrees > 45 && degrees < 90   || degrees < 315 && degrees > 270 ||
  73.         degrees > 135 && degrees < 180 || degrees > 180 && degrees < 225)
  74.      {
  75.        degrees = 90-degrees;
  76.        radians = (pi*degrees)/180;
  77.        tempx*= cos(radians);
  78.        tempy*= sin(radians);
  79.        *x+=(int)tempx;
  80.        *y-=(int)(tempy*aspect_rat);
  81.       }
  82.     }
  83.  
  84. }
  85.  
  86. char *hourstrings[14]={
  87.      "Twelve",
  88.      "One",
  89.      "Two",
  90.      "Three",
  91.      "Four",
  92.      "Five",
  93.      "Six",
  94.      "Seven",
  95.      "Eight",
  96.      "Nine",
  97.      "Ten",
  98.      "Eleven",
  99.      "Twelve",
  100.      "One"};
  101.  
  102. char *minutestrings[31]={
  103.      "O\'clock",
  104.      "One",
  105.      "Two",
  106.      "Three",
  107.      "Four",
  108.      "Five",
  109.      "Six",
  110.      "Seven",
  111.      "Eight",
  112.      "Nine",
  113.      "Ten",
  114.      "Eleven",
  115.      "Twelve",
  116.      "Thirteen",
  117.      "Fourteen",
  118.      "Quarter",
  119.      "Sixteen",
  120.      "Seventeen",
  121.      "Eighteen",
  122.      "Nineteen",
  123.      "Twenty",
  124.      "Twenty-One",
  125.      "Twenty-Two",
  126.      "Twenty-Three",
  127.      "Twenty-Four",
  128.      "Twenty-Five",
  129.      "Twenty-Six",
  130.      "Twenty-Seven",
  131.      "Twenty-Eight",
  132.      "Twenty-Nine",
  133.      "Thirty"};
  134.  
  135.  
  136. void dohands(hour,minute,oldhour,oldminute)
  137. int hour, minute,oldhour,oldminute;
  138. {
  139.    int x1=160, y1=106, x2, y2;
  140.    int hourbase=50, minutebase=64;
  141.    char minister[33];
  142.  
  143.    float hourdegrees, oldhourdegrees;
  144.    float minutedegrees, oldminutedegrees;
  145.  
  146.    minutedegrees=(float)6*minute;
  147.    oldminutedegrees=(float)6*oldminute;
  148.  
  149.        /* update the written display first */
  150.        pen_color(0);
  151.        stuffcords[0][0]=x1-(strlen(timestuff[0])*4);
  152.        move_to(stuffcords[0][0],stuffcords[0][1]);
  153.        plots(timestuff[0]);
  154.        stuffcords[1][0]=x1-(strlen(timestuff[1])*4);
  155.        move_to(stuffcords[1][0],stuffcords[1][1]);
  156.        plots(timestuff[1]);
  157.  
  158.        switch(minute)
  159.        {
  160.           case 0 :  sprintf(timestuff[0],"%s %s",hourstrings[hour],
  161.                                     minutestrings[minute]);break;
  162.           case 15:  sprintf(timestuff[0],"%s after %s",minutestrings[minute],
  163.                                           hourstrings[hour]);break;
  164.           case 30:  sprintf(timestuff[0],"%s %s",hourstrings[hour],
  165.                                     minutestrings[minute]);break;
  166.           case 45: sprintf(timestuff[0],"%s to %s",minutestrings[60-minute],
  167.                                       hourstrings[hour+1]);break;
  168.  
  169.           default:
  170.                    if(minute==1||minute==59)strcpy(minister,"Minute");
  171.                    else strcpy(minister,"Minutes");
  172.                    if(minute<30)
  173.                      sprintf(timestuff[0],"%s %s after %s",
  174.                                           minutestrings[minute],minister,
  175.                                           hourstrings[hour]);
  176.                    else
  177.                      sprintf(timestuff[0],"%s %s to %s",
  178.                                          minutestrings[60-minute],minister,
  179.                                          hourstrings[hour+1]);
  180.                    }
  181.        sprintf(timestuff[1],"%2d:%2d",hour,minute);
  182.        if(timestuff[1][3]==' ')timestuff[1][3]='0';
  183.  
  184.        pen_color(3);
  185.        stuffcords[0][0]=x1-(strlen(timestuff[0])*4);
  186.        move_to(stuffcords[0][0],stuffcords[0][1]);
  187.        plots(timestuff[0]);
  188.        stuffcords[1][0]=x1-(strlen(timestuff[1])*4);
  189.        move_to(stuffcords[1][0],stuffcords[1][1]);
  190.        plots(timestuff[1]);
  191.  
  192.  
  193.    if(hour==12)hour = 0;
  194.    if(oldhour == 12)oldhour = 0;
  195.  
  196.    hourdegrees= (float)30*hour;
  197.    hourdegrees+= (float).5*minute;
  198.  
  199.    oldhourdegrees= (float)30*oldhour;
  200.    oldhourdegrees+= (float).5*oldminute;
  201.  
  202.        /* erase the old hands */
  203.        pen_color(0);
  204.        x2=x1;
  205.        y2=y1;
  206.        circlepoints(&x2,&y2,minutebase,oldminutedegrees);
  207.        move_to(x1,y1);
  208.        line_to(x2,y2);
  209.        x2=x1;
  210.        y2=y1;
  211.        circlepoints(&x2,&y2,hourbase,oldhourdegrees);
  212.        move_to(x1,y1);
  213.        line_to(x2,y2);
  214.  
  215.        /* draw the new hands */
  216.        pen_color(2);
  217.        x2=x1;
  218.        y2=y1;
  219.        circlepoints(&x2,&y2,minutebase,minutedegrees);
  220.        move_to(x1,y1);
  221.        line_to(x2,y2);
  222.        pen_color(1);
  223.        x2=x1;
  224.        y2=y1;
  225.        circlepoints(&x2,&y2,hourbase,hourdegrees);
  226.        move_to(x1,y1);
  227.        line_to(x2,y2);
  228.  
  229.        /* draw a small circle at the center */
  230.        pen_color(3);
  231.        move_to(x1,y1);
  232.        circle(4,3);
  233.  
  234.  
  235. }
  236.  
  237.  
  238.  
  239.  
  240. main()
  241. {
  242.  
  243.        int x1=160, y1=106, x2,y2,circlebase = 70, letterbase=80;
  244.        int markout= 72, markin = 71;
  245.        int xtemp, ytemp;
  246.  
  247.        int done = 0;
  248.        int hour = 12, oldhour=12;
  249.        int minute= 0, oldminute = 0;
  250.        char c;
  251.        char numbuf[40];
  252.        int i, xfactor, yfactor=4;
  253.        float hourdegrees;
  254.  
  255.     /* allow only CGA compatible adapters */
  256.     if(((biosequip() >>4) &3)==3)
  257.     {
  258.         printf("Sorry... Cga Required...\n");
  259.         exit(0);
  260.     }
  261.  
  262.        setvmode(5);
  263.  
  264.        /* set the global aspect ratio */
  265.        getvconfig(&_videostuff);
  266.        aspect_rat =
  267.        (float) _videostuff.aspect_v / (float) _videostuff.aspect_h;
  268.  
  269.        /* outline the screen */
  270.  
  271.        pen_color(2);
  272.        move_to(0,0);
  273.        box(319,21,0);
  274.        pen_color(1);
  275.        move_to(0,22);
  276.        box(319,191-22,0);
  277.  
  278.  
  279.        pen_color(3);
  280.        strcpy(numbuf,"Little Ben(C) 1991 by Bill Buckels");
  281.        xtemp = x1-(strlen(numbuf)*4);
  282.        move_to(xtemp,2);
  283.        plots(numbuf);
  284.        pen_color(1);
  285.        strcpy(numbuf,"Most Keys and Arrows Hot-Esc Exits");
  286.        xtemp = x1-(strlen(numbuf)*4);
  287.        move_to(xtemp,10);
  288.        plots(numbuf);
  289.        pen_color(2);
  290.        strcpy(numbuf,"Made In Canada");
  291.        xtemp = x1-(strlen(numbuf)*4);
  292.        move_to(xtemp,192);
  293.        plots(numbuf);
  294.  
  295.  
  296.  
  297.        /* do all the small marks */
  298.        for(i=0;i<60;i++)
  299.        {
  300.          hourdegrees= (float)6*i;
  301.          x2 = x1;
  302.          y2 = y1;
  303.          xtemp = x1;
  304.          ytemp = y1;
  305.  
  306.          circlepoints(&x2,&y2,markin,hourdegrees);
  307.          if(i%5==0)
  308.          {
  309.             pen_color(1);
  310.             circlepoints(&xtemp,&ytemp,markout+yfactor,hourdegrees);
  311.             }
  312.          else
  313.          {
  314.            circlepoints(&xtemp,&ytemp,markout,hourdegrees);
  315.            pen_color(2);
  316.            }
  317.          move_to(x2,y2);
  318.          line_to(xtemp, ytemp);
  319.          }
  320.  
  321.        /* do the clock face */
  322.        pen_color(3);
  323.        move_to(x1,y1);
  324.        circle(circlebase,3);
  325.  
  326.  
  327.        /* put on the numbers */
  328.        for(i=1;i<13;i++)
  329.        {
  330.          sprintf(numbuf,"%d",i);
  331.          xfactor = strlen(numbuf)*4;
  332.          hourdegrees= (float)30*i;
  333.          x2 = x1;
  334.          y2 = y1;
  335.          circlepoints(&x2,&y2,letterbase,hourdegrees);
  336.          move_to(x2-xfactor,y2-yfactor);
  337.  
  338.          if(i%3==0)pen_color(2);
  339.          else pen_color(1);
  340.  
  341.          plots(numbuf);
  342.          }
  343.  
  344.        /* set the hands with the start value */
  345.        strcpy(timestuff[0],"");
  346.        strcpy(timestuff[1],"");
  347.        dohands(hour,minute,oldhour,oldminute);
  348.  
  349.        do
  350.        {
  351.         /* we can put a timer loop in here */
  352.         if(kbhit())
  353.         {
  354.             switch(getch())
  355.             {
  356.              case 27: setvmode(3);exit(0);
  357.  
  358.              case 0: c=getch();
  359.                      if(c!=UPARROW)
  360.                        if(c!=DOWNARROW)
  361.                          if(c!=LEFTARROW)
  362.                            if(c!=RIGHTARROW)
  363.                                          break;
  364.                      oldhour=hour;
  365.                      oldminute=minute;
  366.  
  367.  
  368.                      if(c==DOWNARROW)hour++;
  369.                      if(c==UPARROW)hour--;
  370.  
  371.                       if(c==RIGHTARROW)
  372.                       {
  373.                         minute=(minute/5)*5; /* truncate to even intervals */
  374.                         minute+=5;
  375.                         if(minute>59)
  376.                         {
  377.                         minute-=60;
  378.                         hour++;
  379.                         }
  380.                         }
  381.  
  382.                       if(c==LEFTARROW)
  383.                       {
  384.                       minute=(minute/5)*5;
  385.                       minute-=5;
  386.                       if(minute<0)
  387.                       {
  388.                        minute+=60;
  389.                        hour--;
  390.                        }
  391.                        }
  392.  
  393.                      if(hour>12)hour=1;
  394.                      if(hour<1)hour=12;
  395.  
  396.                      dohands(hour,minute,oldhour,oldminute);
  397.                      break;
  398.  
  399.  
  400.              default: oldminute=minute;
  401.                       oldhour  =hour;
  402.                       minute++;
  403.                       if(minute>59)
  404.                       {
  405.                         minute=0;
  406.                         hour++;
  407.                         if(hour>12)hour=1;
  408.                         }
  409.                       dohands(hour,minute,oldhour,oldminute);
  410.                       }
  411.                     }
  412.        }
  413.        while(!(done));
  414.        setvmode(3);
  415.        exit(0);
  416.  
  417. }
  418.  
  419.  
  420.  
  421.  
  422.  
  423.